﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.UI;
using CMSMVC4.Areas.Main.Models;
using CMSMVC4.Filters;
using CMSMVC4.MultiResource;

namespace CMSMVC4.Areas.Main.Controllers
{
    public class DichVuController : Controller
    {
        //
        // GET: /Main/DichVu/
        CMSMVC4Entities db = new CMSMVC4Entities();
        CommandSql cm = new CommandSql();


        public ActionResult Index()
        {
            int id;
            //var data = cm.GetIdCategorieByStatus(MenuType.DichVu, PageStatus.GioiThieu);
            var data = db.Categories.OrderByDescending(x => x.Sort).FirstOrDefault(x => x.Type == MenuType.DichVu && x.Status == CategoriStatus.Thuong);
            if (data != null)
            {
                id = data.Id;
                return RedirectToAction("BaiViet", "DichVu", new { id });
            }
            else
            {
                id = 0;
                return RedirectToAction("BaiViet", "DichVu", new { id });
            }
        }


        [ChildActionOnly]
        public ActionResult ListService(string id)
        {
            var data = cm.GetNameCategories(MenuType.DichVu);
            ViewBag.id = id;
            return PartialView(data);
        }

        [ChildActionOnly]
        public ActionResult ListFoder(string id)
        {
            string lag = cm.GetLanguage();
            var data =
                db.CateLangs.OrderByDescending(x => x.Category.Sort).Where(
                    x => x.Category.Type == MenuType.DichVu && x.Category.Status == CategoriStatus.Foder && x.LanguageCode.Equals(lag) && x.Category.paren == 0).ToList();
            int paren = int.Parse(id);
            var cate = db.Categories.FirstOrDefault(x => x.Id == paren);
            if (cate != null)
            {
                ViewBag.paren = cate.paren;
            }
            else
            {
                ViewBag.paren = 0;
            }
            ViewBag.id = id;
            return PartialView(data);
        }
        [ChildActionOnly]
        public ActionResult ShowThietBi(int id, int paren, bool show)
        {
            string lag = cm.GetLanguage();
            var data =
                db.CateLangs.OrderByDescending(x => x.Category.Sort).Where(
                    x => x.Category.Type == MenuType.DichVu && x.Category.paren == paren && x.Category.Status != CategoriStatus.Nhap && x.LanguageCode.Equals(lag)).ToList();
            ViewBag.id = id;
            ViewBag.show = show;
            return PartialView(data);
        }

        [OutputCache(Duration = 900, VaryByParam = "id", Location = OutputCacheLocation.Client, NoStore = true)]
        public ActionResult BaiViet(BaiVietModel model)
        {
            if (ModelState.IsValid)
            {
                var data = cm.GetPageLang(model.id);
                if (data.Rows.Count > 0)
                {
                    ViewBag.Title = data.Rows[0]["Name"].ToString();
                    ViewBag.IdPage = data.Rows[0]["PageId"].ToString();
                    ViewBag.key = data.Rows[0]["Name"].ToString();
                    ViewBag.description = data.Rows[0]["Summary"].ToString();
                }
                ViewBag.Id = model.id;
                //select menu dang chon
                var menu = db.Menus.FirstOrDefault(x => x.Type == MenuType.DichVu);
                if (menu != null)
                {
                    ViewBag.Menu = menu.Id;
                }
                else
                {
                    ViewBag.Menu = 0;
                }
                return View(data);
            }
            return View("Error");
        }
    }
}
